package longrise.phone.com.bjjt_jyb.myaccident;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.Adapter.AccidentAdapter;
import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ApiImpI;
import longrise.phone.com.bjjt_jyb.Utils.DialogUtils;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.compensate.AccidentCheckView;
import longrise.phone.com.bjjt_jyb.compensate.AccidentLoadingPictureView;
import longrise.phone.com.bjjt_jyb.compensate.AccidentLoadingView;
import longrise.phone.com.bjjt_jyb.compensate.CreateAgreementView;
import longrise.phone.com.bjjt_jyb.compensate.SelectAccidentView;


/**
 * 事故刷新列表
 */
public class AccidentListView extends LFView implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {

    private Context mContext;

    private View mView;


    private AccidentAdapter mAdapter;

    private List<EntityBean> beanLists = new ArrayList();

    private int caseType;//0查询未完成；1查询已完成

    private BaseApplication application;

    private View emptyIV;//无数据时的背景色
    private SwipeRefreshLayout sf_history;
    private RecyclerView rv_history;

    public AccidentListView(Context context, int type) {
        super(context);
        this.mContext = context;
        this.caseType = type;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_accidenttlist, null);
        if (mView == null) {
            return;
        }
        emptyIV = mView.findViewById(R.id.empty_tv);
        application = (BaseApplication) mContext.getApplicationContext();
        initSwipeRefreshLayoutAndRecyclerView();
    }

    private void initSwipeRefreshLayoutAndRecyclerView() {
        sf_history = (SwipeRefreshLayout) mView.findViewById(R.id.sf_history);
        sf_history.setOnRefreshListener(this);
        sf_history.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_history = (RecyclerView) mView.findViewById(R.id.rv_history);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_history.setLayoutManager(layoutManager);
        mAdapter = new AccidentAdapter(mContext, caseType, beanLists);
        if (caseType == 0) {
            mAdapter.setOnClickListner(this);
        }
        mAdapter.setOnRecyclerViewItemClickListener(this);
        rv_history.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(View view, int i) {
        loadDetailInfo(beanLists.get(i), 0);
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    public void onClick(View v) {
        EntityBean bean = (EntityBean) v.getTag();
        String state = bean.getString("state");
        String appcaseno = bean.getString("appcaseno");
        LFView dealView = null;
        String isSingle = bean.getString("casetype");
        if (TextUtils.equals("1", state)) {//等待定责
            if ((TextUtils.equals("10", isSingle)) || (TextUtils.equals("11", isSingle))) {
                dealView = new AccidentCheckView(mContext, appcaseno, 3);
            } else {
                dealView = new AccidentCheckView(mContext, appcaseno, 2);
            }
        } else if (TextUtils.equals("2", state)) {//保险报案
            loadDetailInfo(bean, 2);
        } else if (TextUtils.equals("3", state)) {//等待理赔
            dealView = new AccidentCheckView(mContext, appcaseno, 3);
        } else if (TextUtils.equals("4", state)) {//等待评价
           /* String carno = bean.getString("casecarno");
            dealView = new ServiceeValuateView(mContext, appcaseno, carno);*/
        } else if (TextUtils.equals("7", state)) {//审核定责照片
            if ((TextUtils.equals("10", isSingle)) || (TextUtils.equals("11", isSingle))) {
                dealView = new AccidentCheckView(mContext, appcaseno, 1);
            } else {
                dealView = new AccidentCheckView(mContext, appcaseno, 0);
            }
        } else if (TextUtils.equals("8", state)) {//审核定损照片
            dealView = new AccidentCheckView(mContext, appcaseno, 1);
        } else if (TextUtils.equals("9", state)) {//案件信息未上传
            loadCarInfo(bean);
        } else if (TextUtils.equals("10", state)) {//未生成协议书
            loadDetailInfo(bean, 10);
        } else if (TextUtils.equals("11", state)) {//定损审核（定损报告书）
            loadDetailInfo(bean, 11);
        } else if (TextUtils.equals("12", state)) {//未上传银行卡信息
            loadDetailInfo(bean, 12);
        } else if (TextUtils.equals("13", state)) {/*//定损图片未上传
            String isSingle = bean.getString("casetype");
            if (TextUtils.equals("0", isSingle)) {
                dealView = new SingleCarLossView(mContext, appcaseno, 0);
            } else {
                dealView = new LoadingCarLossView(mContext, appcaseno, 0);
            }*/
        } else if (TextUtils.equals("14", state)) {//定责图片未上传
            loadPasImg(appcaseno, isSingle);
        }
        if (null != dealView) {
            FrameworkManager.getInstance().showNewForm(mContext, dealView);
        }
    }

    //获取定责审核过的图片
    private void loadPasImg(final String appcaseno, final String isSingle) {
        String uid = application.getKckpName();
        String token = application.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).getPasImgUrl(uid, token, appcaseno, "1", new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (null == mView) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    if (null != dataBean) {
                        EntityBean[] unBeans = dataBean.getBeans("notpasslist");
                        EntityBean[] isBeans = dataBean.getBeans("passlist");
                        if ("0".equals(isSingle)) {//单车
                            AccidentLoadingPictureView view = new AccidentLoadingPictureView(mContext, appcaseno, 1, 0);
                            view.setUnqualifylist(unBeans);
                            view.setIsqualifylist(isBeans);
                            FrameworkManager.getInstance().showNewForm(mContext, view);
                        } else if ("1".equals(isSingle)) {
                            AccidentLoadingPictureView view = new AccidentLoadingPictureView(mContext, appcaseno, 1, 1);
                            view.setUnqualifylist(unBeans);
                            view.setIsqualifylist(isBeans);
                            FrameworkManager.getInstance().showNewForm(mContext, view);
                        } else if ("10".equals(isSingle)) {
                            AccidentLoadingPictureView view = new AccidentLoadingPictureView(mContext, appcaseno, 1, 2);
                            view.setUnqualifylist(unBeans);
                            view.setIsqualifylist(isBeans);
                            FrameworkManager.getInstance().showNewForm(mContext, view);
                        } else if ("11".equals(isSingle)) {
                            AccidentLoadingPictureView view = new AccidentLoadingPictureView(mContext, appcaseno, 1, 3);
                            view.setUnqualifylist(unBeans);
                            view.setIsqualifylist(isBeans);
                            FrameworkManager.getInstance().showNewForm(mContext, view);
                        }
                        return;
                    }
                }
                String error = bean.getString("redes");
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }
        });
    }

    //下载案件详情
    private void loadDetailInfo(final EntityBean caseBean, final int type) {
        final String casenumber = caseBean.getString("casenumber");
        EntityBean baseBean = application.getLoginInfo().getBean("userinfo");
        String phone = baseBean.getString("mobilephone");
        String username = application.getKckpName();
        String token = application.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).accidentDetails(casenumber, phone, username, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(getContext(), error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            EntityBean dataBean = bean.getBean("data");
                            EntityBean[] infoBeans = dataBean.getBeans("casecarlist");
                            String appcaseno = caseBean.getString("appcaseno");
                            String isSingle = caseBean.getString("casetype");
                            String disposetype = dataBean.getString("kckpjjAccidentDetails");
                            LFView dealView = null;
                            if (type == 0) {//进入事故 详情界面
                                dealView = new AccidentDetailView(mContext, caseBean, dataBean);
                            } else if (type == 2) {/*//进入保险报案选择界面
                                dealView = new InsuranceReportChoiceView(mContext, infoBeans, appcaseno);
                                ((InsuranceReportChoiceView) dealView).setIfSingle(isSingle);
                            */
                            } else if (type == 10) {//生成协议书界面
                                /*if("1".equals(disposetype))
                                dealView = new CreateAgreementView(mContext, infoBeans, appcaseno,0);
                                else dealView = new CreateAgreementView(mContext, infoBeans, appcaseno,1);*/
                                dealView = new CreateAgreementView(mContext, infoBeans, appcaseno, Integer.valueOf(isSingle));
                            } else if (type == 11) {/*//定损报告书界面
                                dealView = new CheckLossReportView(mContext, infoBeans, appcaseno);
                                ((CheckLossReportView) dealView).setUnqualifylist(isSingle);
                           */
                            } else if (type == 12) {/*//银行卡信息界面
                                if (TextUtils.equals("0", isSingle)) {//单车流程
                                    dealView = new BankCardInfoView(mContext, infoBeans, appcaseno, "0");
                                } else {
                                    dealView = new BankCardInfoView(mContext, infoBeans, appcaseno);
                                }*/
                            }
                            if (null != dealView) {
                                FrameworkManager.getInstance().showNewForm(mContext, dealView);
                            }
                        } else {
                            //String redes = bean.getString("redes");
                            String redes = "该案件无办理详情！";
                            UiUtil.showToast(mContext, redes);
                        }
                    }
                });
    }

    //获取车辆信息
    private void loadCarInfo(final EntityBean caseBean) {
        EntityBean userInfo = application.getLoginInfo().getBean("userinfo");
        String userName = application.getKckpName();
        String carownername = userInfo.getString("name");
        String token = application.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(application).getCarOwner(carownername, userName, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(getContext(), error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean cardBean = bean.getBean("data");
                    String appcaseno = caseBean.getString("appcaseno");
                    String isSingle = caseBean.getString("casetype");
                    LFView dealView = null;
                    if (TextUtils.equals("0", isSingle)) {//单车事故上传
                        dealView = new AccidentLoadingView(mContext, appcaseno);
                    } else if (TextUtils.equals("10", isSingle)) {
                        dealView = new SelectAccidentView(mContext, appcaseno, 2);
                    } else if (TextUtils.equals("11", isSingle)) {
                        dealView = new SelectAccidentView(mContext, appcaseno, 3);
                    } else {
                        //选择事故类型界面-->填写事故信息-->有无争议界面
                        dealView = new SelectAccidentView(mContext, appcaseno, 1);
                    }
                    if (null != dealView) {
                        FrameworkManager.getInstance().showNewForm(mContext, dealView);
                    }
                } else {
                    String redes = bean.getString("redes");
                    UiUtil.showToast(mContext, redes);
                }
            }
        });
    }

    //加载数据
    public void loadData() {
        EntityBean baseBean = application.getLoginInfo().getBean("userinfo");
        String userId = baseBean.getString("id");
        String phone = baseBean.getString("mobilephone");
        String username = application.getKckpName();
        String token = application.getToken();
        ApiImpI.getInstance(application).searchAllCase(userId, phone, String.valueOf(caseType),
                username, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        if (null != sf_history && sf_history.isRefreshing()) {// 停止刷新
                            sf_history.setRefreshing(false);
                        }
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (null != sf_history && sf_history.isRefreshing()) {// 停止刷新
                            sf_history.setRefreshing(false);
                        }
                        DialogUtils.closeDialog();
                        beanLists.clear();//重置数据
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals("1", restate)) {
                            EntityBean[] entityBeans = bean.getBeans("data");
                            if(null==entityBeans)return;
                            for (EntityBean e : entityBeans) {
                                if(null!=e) beanLists.add(e);
                            }
                        }
                        if (null == beanLists || beanLists.size() <= 0) {
                            emptyIV.setVisibility(View.VISIBLE);
                            mAdapter.setEmptyView(emptyIV);
                        } else {
                            emptyIV.setVisibility(View.GONE);//null
                        }
                        if (null != mAdapter) {
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    @Override
    public void refresh() {
        DialogUtils.showDialog(mContext);
        loadData();
    }

    @Override
    public FormParameter getFormParameter() {
        return null;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (null != mAdapter) {
            mAdapter.setOnRecyclerViewItemClickListener(null);
            mAdapter.setOnLoadMoreListener(null);
            mAdapter = null;
        }
        if (null != rv_history) {
            rv_history.setAdapter(null);
            rv_history = null;
        }
        beanLists = null;
        mView = null;
        DialogUtils.destroyDialog();
    }
}

